package com.flame.parameter.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.parameter.controller.vo.DictionaryItemVo;
import com.flame.parameter.controller.vo.DictionaryVo;
import com.flame.parameter.entity.Dictionary;
import com.flame.parameter.entity.DictionaryItem;
import com.flame.parameter.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * @author wuxintong
 * @since 2022/10/26
 */
@RestController
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @HasAuth("dict")
    @PostMapping("/dictionary/page")
    public PageResult page(@RequestBody DictionaryVo dictionaryVo) {
        return PageResult.success(dictionaryService.pageDictionary(dictionaryVo));
    }

    @HasAuth("dict:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/add")
    public Result addDictionary(@RequestBody @Validated Dictionary dictionary) {
        dictionaryService.addDictionary(dictionary);
        return Result.success();
    }

    @HasAuth("dict:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/update")
    public Result updateDictionary(@RequestBody @Validated Dictionary dictionary) {
        dictionaryService.updateDictionary(dictionary);
        return Result.success();
    }

    @HasAuth("dict:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/delete")
    public Result deleteDictionary(@RequestBody @Validated Dictionary dictionary) {
        dictionaryService.deleteDictionary(dictionary.getDictCode());
        return Result.success();
    }

    @HasAuth("dict")
    @PostMapping("/dictionary/group/query-all")
    public Result queryDictionaryGroup(Dictionary dictionary) {
        return Result.success(dictionaryService.queryDictionaryGroup(dictionary.getDictGroup()));
    }

    @PostMapping("/dictionary/item/page")
    public PageResult itemPage(@RequestBody DictionaryItemVo dictionaryItemVo) {
        return PageResult.success(dictionaryService.pageDictionaryItem(dictionaryItemVo));
    }

    @HasAuth("dict:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/item/add")
    public Result addDictionaryItem(@RequestBody @Validated DictionaryItem dictionaryItem) {
        dictionaryService.addDictionaryItem(dictionaryItem);
        return Result.success();
    }

    @HasAuth("dict:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/item/update")
    public Result updateDictionaryItem(@RequestBody @Validated DictionaryItem dictionaryItem) {
        dictionaryService.updateDictionaryItem(dictionaryItem);
        return Result.success();
    }

    @HasAuth("dict:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dictionary/item/delete")
    public Result deleteDictionaryItem(@RequestBody @Validated DictionaryItem dictionaryItem) {
        dictionaryService.deleteDictionaryItem(dictionaryItem.getDictCode(), dictionaryItem.getDictItemValue());
        return Result.success();
    }
}
